package com.mew.slots.core.game.mnyys.room;

import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.game.mnyys.dto.MnyysInRoom;
import com.mew.slots.core.game.mnyys.dto.MnyysPlayMsgs;
import com.mew.slots.core.game.mnyys.roller.Controlline;

import java.util.ArrayList;

/**
 * @author dylin
 */
public class MnyysRoom extends BaseRoom {

    public static int _FREE = 13;
    public static int _SCATTER = 14;
    public static int _GOLDEN_FREE = 15;

    public int progress_max = 200;
    public int progress_add = 2;

    private MnyysInRoom mnyysInRoom;
    public MnyysRoom(MnyysInRoom mnyysInRoom){
        this.mnyysInRoom=mnyysInRoom;
        res = new MnyysPlayMsgs();
        row = 4;
        col = 6;
        _WILD = 12;
        spes = new ArrayList<Integer>(){{
            add(12);
            add(13);
            add(14);
            add(15);
        }};

         rate = new int[][]{
                 {0,  0,  5,  10,  30,  50},//9                  0
                 {0,  0,  5,  10,  30,  50},//10                 1
                 {0,  0,  5,  10,  30,  50},//J                  2
                 {0,  0,  5,  10,  30,  50},//Q                  3
                 {0,  0,  5,  10,  30,  50},//K                  4
                 {0,  0,  5,  10,  30,  50},//A                  5
                 {0,  0, 10,  20,  50, 100},//烛灯                6
                 {0,  0, 10,  20,  50, 100},//戒指                7
                 {0,  0, 10,  20,  50, 100},//咖啡                8
                 {0,  0, 12,  30,  75, 150},//女仆                9
                 {0,  0, 12,  30,  75, 150},//美女                10
                 {0,  5, 15,  40, 100, 200},//野兽                11

                 {0,  5, 15,  40, 100, 200},//WILD               12
                 {0,  0,  0,   0,   0,   0},//FREE               13
                 {0,  0,  0,   0,   0,   0},//SCATTER            14
                 {0,  0,  0,   0,   0,   0},//GOLD_FREE          15
        };

         alllines = new int[][]{
                 {6,7,8,9,10,11},
                 {12,13,14,15,16,17},
                 {0,1,2,3,4,5},
                 {18,19,20,21,22,23},
                 {0,7,14,15,10,5},
                 {18,13,8,9,16,23},
                 {6,13,20,21,16,11},
                 {12,7,2,3,10,17},
                 {0,7,2,3,10,5},
                 {18,13,20,21,16,23},
                 {6,1,8,9,4,11},
                 {12,19,14,15,22,17},
                 {6,13,8,9,16,11},
                 {12,7,14,15,10,17},
                 {0,1,8,9,4,5},
                 {18,19,14,15,22,23},
                 {6,7,14,15,10,11},
                 {12,13,8,9,16,17},
                 {12,13,20,21,16,17},
                 {6,7,2,3,10,11},
                 {6,13,14,15,16,11},
                 {12,7,8,9,10,17},
                 {6,1,2,3,4,11},
                 {12,19,20,21,22,17},
                 {18,13,14,15,16,23},
                 {0,7,8,9,10,5},
                 {12,1,2,3,4,17},
                 {6,19,20,21,22,11},
                 {18,7,8,9,10,23},
                 {0,13,14,15,16,5},
                 {12,13,2,3,16,17},
                 {6,7,20,21,10,11},
                 {0,1,14,15,4,5},
                 {18,19,8,9,22,23},
                 {18,19,2,3,22,23},
                 {0,1,20,21,4,5},
                 {18,13,8,3,4,5},
                 {0,7,14,21,22,23},
                 {0,1,2,9,16,23},
                 {18,19,20,15,10,5},
                 {12,7,2,3,4,5},
                 {6,13,20,21,22,23},
                 {0,1,2,3,10,17},
                 {18,19,20,21,16,11},
                 {18,13,8,9,10,11},
                 {0,7,14,15,16,17},
                 {6,7,8,9,16,23},
                 {12,13,14,15,10,5},
                 {6,19,2,3,22,11},
                 {12,1,20,21,4,17},
                 {0,19,2,3,22,5},
                 {18,1,20,21,4,23},
                 {0,7,2,9,4,11},
                 {18,13,20,15,22,17},
                 {6,13,8,15,10,17},
                 {12,7,14,9,16,11},
                 {12,19,14,21,16,23},
                 {6,1,8,3,10,5},
                 {0,13,2,15,4,17},
                 {18,7,20,9,22,11},
                 {6,19,8,21,10,23},
                 {12,1,14,3,16,5},
                 {0,19,2,21,4,23},
                 {18,1,20,3,22,5},
                 {0,7,14,21,16,11},
                 {18,13,8,3,10,17},
                 {6,13,20,15,10,5},
                 {12,7,2,9,16,23},
                 {0,7,2,9,16,23},
                 {18,13,20,15,10,5},
                 {6,13,8,3,10,17},
                 {12,7,14,21,16,11},
                 {0,19,20,21,22,5},
                 {18,1,2,3,4,23},
                 {6,1,8,15,10,5},
                 {12,19,14,9,16,23},
                 {0,13,20,21,16,5},
                 {18,7,2,3,10,23},
                 {6,19,2,21,4,23},
                 {12,1,20,3,22,5}
         };

    }
    /**
     * 取线
     */
    @Override
    public void getLinesXml(){

        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = rollerXml.getLineInts(free?1:0,rollerID);
        }
        res.endSlots=endSlots.clone();
        if(free){
            replaceWild();
        }
    }

    private void replaceWild(){
        for(int index:mnyysInRoom.getGoldenFrees()){
            endSlots[index]=_WILD;
        }
    }

    @Override
    public void freeBouns() {
        computeFree();
        computeBonus();
    }
    private void computeFree(){
        MnyysPlayMsgs pmres=(MnyysPlayMsgs) res;
        int freeNum=0;
        for (int c = 0; c <col ; c++) {
            boolean noHasFree=true;
            for (int r = 0; r < row; r++) {
                int index=r*col+c;
                if(endSlots[index]==_FREE||endSlots[index]==_GOLDEN_FREE){
                    if(endSlots[index]==_GOLDEN_FREE){
                        pmres.getGoldenFrees().add(index);
                    }
                    freeNum++;
                    noHasFree=false;
                }
            }
            if(noHasFree){
                break;
            }
        }
        if(freeNum>4){
            mnyysInRoom.setGoldenFrees(pmres.getGoldenFrees());
            int hasFree=(freeNum-5)*5+5;
            res.setFree((byte)1);
            res.setFreeNumber(hasFree>100?100:hasFree);
        }else{
            pmres.getGoldenFrees().clear();
        }
    }
    private void computeBonus(){
        int[] courses=null;
        for (int[] cours : mnyysInRoom.getProgressBar()) {
            if(cours[0]==(int)res.getBet()){
                courses=cours;
                break;
            }
        }
        if(courses==null){
            courses=new int[]{
                    (int)res.getBet(),0
            };
            mnyysInRoom.getProgressBar().add(courses);
        }
        MnyysPlayMsgs pmres=(MnyysPlayMsgs) res;
        int snum=fsdb(_SCATTER);
        courses[1]=courses[1]+snum*progress_add;
        if(courses[1]>=progress_max){
            pmres.setBouns(1);
            courses[1]=progress_max;
        }
        pmres.setProgressBar(courses[1]);
    }

    @Override
    public int[] control(){
        Controlline controlline=new Controlline();
        switch (controlType) {
            case 1:
                return controlline.big();
            case 2:
                return controlline.free();
            case 3:
                return controlline.bonuds();
            default:
                return controlline.empty();
        }
    }
}
